<?php
require_once 'config21.php';
if (!isset($_SESSION['userid'])) {
    header("Location: login21.php");
    exit;
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_nationalbird'])) {
    $ccountry=$_POST['ccountry'];
    $country=$_POST['country'];
    $cname=$_POST['cname'];
    $name=$_POST['name'];

    if(empty($_FILES['picture']['name'])) {
        echo "文件名不能为空";
        exit;
    }
    $picture = $_FILES['picture']['name'];  // 获取文件名
    $filetype=substr($picture,strrpos($picture,"."),strlen($picture)-strrpos($picture,"."));  //获取文件后缀名
    if($filetype=='.jpg' || $filetype=='.png' || $filetype=='.gif' || $filetype=='.JPG' || $filetype=='.PNG' || $filetype=='.GIF') {
        if($_FILES['picture']['size'] > 1000000) {
            echo "<script>alert('文件太大，不能上传');</script>";
            echo "<script>location.href='insert1.php';</script>";
            exit;
        }
        $savedir="images/". $picture;  // 文件存放目录
        if(move_uploaded_file($_FILES['picture']['tmp_name'],$savedir)) {
            $sql="INSERT INTO nationalbird(ccountry,country,cname,name,picture)
            VALUES('$ccountry','$country','$cname','$name','$savedir')";
            if ($conn->query($sql) === TRUE) {
                header("Location: list_nationalbird21.php");
                exit;
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
        }
    } else {
        echo "<script>alert('文件类型错误');</script>";
        echo "<script>location.href='insert1.php';</script>";
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加国鸟</title>
    <link rel="stylesheet" href="style21.css">
</head>
<body>
    <form method="post" enctype="multipart/form-data" name="form1" id="form1">
        <table width="400">
            <tbody>
                <tr>
                    <td colspan="2" align="center">添加国鸟信息</td>
                </tr>
                <tr>
                    <td>国籍</td>
                    <td><input type="text" name="ccountry" id="ccountry"></td>
                </tr>
                <tr>
                    <td>国籍(英文)</td>
                    <td><input type="text" name="country" id="country"></td>
                </tr>
                <tr>
                    <td>鸟名</td>
                    <td><input type="text" name="cname" id="cname"></td>
                </tr>
                <tr>
                    <td>鸟名(英文)</td>
                    <td><input type="text" name="name" id="name"></td>
                </tr>
                <tr>
                    <td>上传照片</td>
                    <td><input type="file" name="picture" id="picture"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" name="add_nationalbird" value="添加">
                        &nbsp;&nbsp;
                        <input type="reset" name="reset" id="reset" value="重置">
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>